package ModelImplementation

import (
	"PaySalary/Model"
	"time"
)

type CommissionedClassification struct {
	itCommissionRate float64
	itsSalary float64
	itsReceipts map[time.Time]SalesReceipt
}

func commissionedClassification(salary float64,commissionalRate float64) CommissionedClassification{
	return CommissionedClassification{itsSalary: salary,itCommissionRate: commissionalRate}
}

func (cc* CommissionedClassification) GetSalary() float64 {
	return cc.itsSalary
}

func (cc* CommissionedClassification) AddSalesReceipt(sr SalesReceipt) {
	cc.itsReceipts[sr.GetSaleDate()] = sr
}

func (cc* CommissionedClassification) GetReceipt(date time.Time) SalesReceipt {
	return cc.itsReceipts[date]
}

func (cc* CommissionedClassification) GetRate() float64 {
	return cc.itCommissionRate
}

func (cc* CommissionedClassification) CalculatePay(pc Model.Paycheck) float64 {
	commission := 0.0
	for date := range cc.itsReceipts {
		receipt := cc.itsReceipts[date]
		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()
		if date.After(dt1) && date.Before(dt2) {
			commission += receipt.GetAmount() * cc.itCommissionRate
		}
	}
	return cc.itsSalary + commission
}
